var config = require('../uteil')
const { token, url } = config;

// 1.获取有效token
var getAccess = require('../model/get_access_token')
// 2.获取jsapi_ticket
var getJsapiTicket = require('../model/get_jsapi_ticket')


//生成签名
var sign = require('../model/sign')

module.exports = async (req, res, next) => {
    
    // console.log('域名：', req.url);
    // console.log('codeData',req.query);
    
    // req.url.split('?')
    let strStart = req.url.indexOf('http')
    let str = req.url.substring(strStart)
    // console.log(str);
    

    // 1.拿到一个可用的accessToken
   let accessToken = await getAccess.fetchAccessToken();
    // console.log(new Date(accessToken.expires_in));

	let	jsapi_ticketData = await getJsapiTicket.fetchJsapiTicket(accessToken.access_token)
    // console.log('加密前：', jsapi_ticketData);

    //签名
    // let data = sign(jsapi_ticketData.ticket, url + ':3001/wechat')
    let data = sign(jsapi_ticketData.ticket,str)
    // console.log('加密后：', data)
    data.appid = 'wxb7a17349f5e9f989'

    res.json({ data })

}